<template>
  <div class="commission-wrap">
    <el-form>
      <el-form-item label-width="150px" label="请选择会员等级：">
        <el-select v-model="value" placeholder="请选择会员等级">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.label"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="150px" label="请设置佣金比例：">
        <el-input v-model="price" type="text" class="input-price"/>
      </el-form-item>
    </el-form>
    <el-row class="row">
      <el-button type="primary" class="btn" @click="handleSubmit" >确认</el-button>
    </el-row>
  </div>
</template>
<script>
import { update } from '@/api/data'
export default {
  name: 'BasicCommission',
  data() {
    return {
      value: '服务商',
      options: [
        {
          value: '选项一',
          label: '服务商'
        },
        {
          value: '选项二',
          label: '区域服务商'
        },
        {
          value: '选项三',
          label: '代理商'
        }
      ],
      price: ''
    }
  },
  methods: {
    handleSubmit() {
      const flag = this.check()
      flag ? this.modification() : ''
    },
    check() {
      const price = this.price
      if (price.trim() === '') {
        this.$message.warning({
          duration: 1000,
          message: '价格不能为空'
        })
        return false
      }
      return true
    },
    modification() {
      const ROLE = ['服务商', '区域服务商', '代理商']
      const id = this.value === ROLE[0] ? '4' : this.value === ROLE[1] ? '6' : '2'
      const name = this.price
      update(id, name)
        .then((res) => {
          if (res.success) {
            this.$message.success({
              duration: 1000,
              message: '设置成功'
            })
          }
        }).catch((e) => {
          this.$message.warning({
            duration: 1000,
            message: '请稍后再试'
          })
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.commission-wrap {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .row {
    padding-left: 150px;
    box-sizing: border-box;
    .btn {
      width: 200px;
    }
  }
  .input-price {
    width: 200px;
  }
}
</style>
